package com.crm.util;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.yunpian.sdk.model.ResultDO;
import com.yunpian.sdk.model.SendBatchSmsInfo;
import com.yunpian.sdk.model.SendSingleSmsInfo;
import com.yunpian.sdk.service.TplOperator;
import com.yunpian.sdk.service.YunpianRestClient;

/**
 * 短信发送工具
 * 
 * @author zealot
 *
 */
public class SMSUtil {
	/**
	 * 云片网短信平台注册服务
	 */
	public static YunpianRestClient client = new YunpianRestClient("d31545b939bae26767d9924684a422fb");

	private static TplOperator tplOperator = null;
	// 编码格式。发送编码格式统一用UTF-8
	private static String ENCODING = "UTF-8";
	// 模板添加
	private static String URI_TEMP_ADD = "https://sms.yunpian.com/v2/tpl/add.json";
	// 智能匹配模版发送接口的http地址
	private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
	// 智能匹配模版批量发送接口的http地址
	private static String URI_SEND_BACTH_SMS = "https://sms.yunpian.com/v2/sms/batch_send.json";
	// appkey
	private static String apikey = "d31545b939bae26767d9924684a422fb";

	/**
	 * 
	 * @Description : TODO
	 * @author zealot
	 * @param @param
	 *            phone
	 * @param @param
	 *            val
	 * @param @return
	 * @return ResultDO<SendSingleSmsInfo>
	 * @version 创建时间 : 2016年12月22日 上午10:02:39
	 * @thorws
	 */
	public static ResultDO<SendSingleSmsInfo> sendSingleSms(String phone, String val) {
		return client.getSmsOperator().singleSend(phone, val);
	}

	/**
	 * 智能匹配模板接口发短信
	 *
	 * @param apikey
	 *            apikey
	 * @param text
	 *            短信内容
	 * @param mobile
	 *            接受的手机号
	 * @return json格式字符串
	 * @throws IOException
	 */

	public static String sendSms(String mobile,String text) throws IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("apikey", apikey);
		params.put("text", text);
		params.put("mobile", mobile);
		return post(URI_SEND_SMS, params);
	}

	public static String post(String url, Map<String, String> paramsMap) {
		CloseableHttpClient client = HttpClients.createDefault();
		String responseText = "";
		CloseableHttpResponse response = null;
		try {
			HttpPost method = new HttpPost(url);
			if (paramsMap != null) {
				List<NameValuePair> paramList = new ArrayList<NameValuePair>();
				for (Map.Entry<String, String> param : paramsMap.entrySet()) {
					NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
					paramList.add(pair);
				}
				method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
			}
			response = client.execute(method);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				responseText = EntityUtils.toString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return responseText;
	}

	public static void main(String[] args) throws IOException {
		String str = "【学慧网】您好！我是学慧网的15321315494老师，今天对您咨询的15321315494方面的课程进行了电话回复，但由于您没有接听到电话，很抱歉未能及时给您提供帮助与解答，我的联系电话是15321315494，您方便的话可以随时联系我，或者根据给您发送的分校路径信息直接到校区咨询，以方便您及时了解相关报考和学习信息，学习像呼吸一样自然！";
		System.out.println(sendSms("15321315494", str));
	}
}
